// pages/tangyouquan/tangyouquan.js
const app = getApp();
var njzxw = require('../../../utils/njzxw.js');
var url = require('../../../utils/url.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: 2,
    tab: [
      {
        name: '最新',
        url: '0',
        active: false,
        fun: 'redirectTos'
      },
      {
        name: '最热',
        url: '1',
        active: false,
        fun: 'redirectTos'
      },
      {
        name: '全部',
        url: '2',
        active: true,
        fun: 'redirectTos'
      },
      {
        name: '饮食推荐',
        url: '/pages/tangyouquan/yinsituijian/yinsituijian',
        active: false,
        fun: 'navigateTo'
      }
    ],
    list: [],
    pageNum: 0
  },
  navigateToss:function(e){
    var $this = this;
    var urls = njzxw.getDataVal(e,'url');
    var id = njzxw.getDataVal(e, 'id');
    wx.showActionSheet({
      itemList: ['查看','删除'],
      success:function(res){
        if (res.tapIndex == 0){
          wx.navigateTo({
            url: urls,
          })
        } else {
          njzxw.alert({
            msg: '确定要删除吗？',
            confirm:true,
            fun:function(a){
              if(a){
                njzxw.ajax({
                  url: url.dynamicInfo.delete,
                  load:true,
                  loadTitle:'删除中',
                  data:{
                    ids:[id]
                  },
                  fun:function(a,b){
                    wx.hideLoading();
                    if(a){
                      if(b.data.state == 1){
                        njzxw.alert({
                          msg: '删除成功',
                          fun:function(){
                            njzxw.loadStart($this);
                            $this.loadData($this);
                          }
                        });
                      }else{
                        njzxw.alert({
                          error: true,
                          msg: '删除失败'
                        });
                      }
                    }else{
                      njzxw.alert({
                        error:true,
                        msg:'删除失败'
                      });
                    }
                  }
                });
              }
            }
          });
        }
      }
    })
  },
  redirectTos: function (e) {
    var urls = njzxw.getDataVal(e, 'url');
    if (!njzxw.isEmpty(urls)) {
      this.data.tab[this.data.url].active = false;
      this.data.tab[urls].active = true;
      var $this = this;
      this.setData({
        url: urls,
        tab: $this.data.tab
      });
      njzxw.loadStart(this);
      this.loadData(this);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    njzxw.initFun(this);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    njzxw.loadStart(this);
    this.loadData(this);
  },
  /**
   * 加载数据信息
   */
  loadData: function ($this) {
    var $this = this;
    njzxw.loadData($this, url.dynamicInfo.queryPage, {
      orderKey: $this.data.url == 0 ? 'a.ishost desc,a.oper_date desc' : $this.data.url == 1 ? 'a.ishost desc,a.praise_num desc' : 'a.ishost desc',
      dynamic_type: 1,
      pageSize: 10,
      type:1
    }, function (dd) {
      for (var i = 0; i < dd.length; i++) {
        dd[i].huifu = " " + njzxw.timeago_(dd[i].hf_oper_date) + " 回复";
        dd[i].image = dd[i].hasOwnProperty('path') ? dd[i].path[0] : '';
      }
      return dd;
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    njzxw.loadStart(this);
    this.loadData(this);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData(this);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})